package cn.lenchu.web.controller;

import cn.lenchu.core.model.GroupDetail;
import cn.lenchu.core.model.GroupItem;
import cn.lenchu.core.model.GroupSummary;
import cn.lenchu.core.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lenchu
 */
@RestController
@RequestMapping("/api/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @PostMapping
    public GroupSummary upsertGroup(Integer userId, Integer groupId, String groupName, Double cash) {
        return groupService.upsertGroup(userId, groupId, groupName, cash);
    }

    @GetMapping("/list")
    public List<GroupSummary> listUserGroup(Integer userId) {
        return groupService.listUserGroup(userId);
    }

    @GetMapping("/detail")
    public GroupDetail groupDetailById(Integer groupId) {
        return groupService.groupDetailById(groupId);
    }

    @GetMapping("/refresh")
    public GroupDetail refreshGroup(Integer groupId) {
        return groupService.refreshGroup(groupId);
    }

    @DeleteMapping
    public GroupDetail deleteGroupById(Integer groupId) {
        return groupService.deleteGroupById(groupId);
    }

    @PostMapping("/item")
    public GroupItem createGroupItem(Integer groupId, String code, String name, Double count, Double price, Integer sort) {
        return groupService.createGroupItem(groupId, code, name, count, price, sort);
    }

    @PutMapping("/item")
    public GroupItem updateGroupItem(Integer id, Double count, Double price, Integer sort) {
        return groupService.updateGroupItem(id, count, price, sort);
    }

    @DeleteMapping("/item")
    public GroupItem deleteGroupItem(Integer id) {
        return groupService.deleteGroupItem(id);
    }
}
